/* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 
Copyright (C) 2010 by Tomasz Pluskiewicz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

using System;
using System.Collections.Generic;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Descriptors;

namespace Yama.MVC.MonoRail.Attributes
{
    ///<summary>
    /// Based on the Convention over configuration principle, this attribute works just like
    /// <see cref="ResourceAttribute"/>, but instead of just one reosurce, adds resources for 
    /// the selected actions to the controller.
    /// 
    /// For example let's assume that the actionNames argument contains an action called 
    /// "Index", current controller's name is "Home", its area "User" 
    /// and resources are stored in assemby "My.Assembly.Rails". 
    /// 
    /// In such case a resource called Index will be added for a resource located at:
    /// 
    /// My.Assembly.Rails.User.Home.Index
    ///</summary>
    public class ConventionResourceAttribute : Attribute, IResourceDescriptorBuilder
    {
        private readonly string[] _actions;

        ///<summary>
        /// Name of abbembly containing resources
        ///</summary>
        public string ResourcesAssemblyName { get; set; }
        /// <summary>
        /// Gets or sets the name of the culture.
        /// </summary>
        /// <value>The name of the culture.</value>
        public String CultureName { get; set; }
        /// <summary>
        /// Gets or sets the type of the resource.
        /// </summary>
        /// <value>The type of the resource.</value>
        public Type ResourceType { get; set; }
        ///<summary>
        /// Area name of the current controller
        ///</summary>
        public string Area { get; set; }
        ///<summary>
        /// Current controller's name
        ///</summary>
        public string Controller { get; set; }

        string BuildResourceName(string actionName)
        {
            const string format = "{0}{1}.{2}.{3}";

            string area = "";
            if (!string.IsNullOrEmpty(this.Area))
            {
                area = "." + this.Area.Replace('\\', '.').Replace('/', '.');
            }

            return string.Format(format, this.ResourcesAssemblyName, area, this.Controller, actionName);
        }

        /// <summary>
        /// Creates a new instance of ConventionResourceAttribute
        /// </summary>
        /// <param name="assemblyName">Assembly containing resources</param>
        /// <param name="controller">Controller's name</param>
        /// <param name="actions">Controller's actions localized in the selected assembly</param>
        public ConventionResourceAttribute(string assemblyName, string controller, params string[] actions)
        {
            _actions = actions;
            this.ResourcesAssemblyName = assemblyName;
            this.Controller = controller;
        }

        /// <summary>
        /// <see cref="IResourceDescriptorBuilder"/> implementation.
        /// Builds the resource descriptors.
        /// </summary>
        public ResourceDescriptor[] BuildResourceDescriptors()
        {
            List<ResourceDescriptor> descriptors = new List<ResourceDescriptor>();

            foreach (string actionName in _actions)
            {
                string resourceName = BuildResourceName(actionName);

                descriptors.Add(new ResourceDescriptor(this.ResourceType,
                                                       actionName,
                                                       resourceName,
                                                       this.CultureName,
                                                       this.ResourcesAssemblyName));
            }

            return descriptors.ToArray();
        }
    }
}